package com.kingyea.app.market.authorize.service.impl;

import com.kingyea.app.market.auth.bean.Authorize;
import com.kingyea.app.market.authorize.dao.AuthorizeMapper;
import com.kingyea.app.market.authorize.service.AuthorizeService;
import com.kingyea.mobilepolice.user.bean.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
@Transactional
public class AuthorizeServiceImpl implements AuthorizeService {


    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorizeServiceImpl.class);

    @Resource
    private AuthorizeMapper authorizeMapper;

    @Override
    public Authorize getById(String accreditId) throws Exception {
        // TODO Auto-generated method stub
        return authorizeMapper.getById(accreditId);
    }

    @Override
    public int deleteById(String accreditId) throws Exception {
        // TODO Auto-generated method stub
        authorizeMapper.deleteById(accreditId);
        return 1;
    }

    @Override
    public Authorize save(Authorize obj) throws Exception {
        // TODO Auto-generated method stub
        if (StringUtils.isEmpty(obj.getAccreditId())) {
            obj.setAccreditId(UUID.randomUUID().toString().replaceAll("-", ""));
        }
        obj.setCreateDate(new Date(System.currentTimeMillis()));
        authorizeMapper.save(obj);
        return obj;
    }

    @Override
    public List<Authorize> list(Map<String, Object> obj) throws Exception {
        // TODO Auto-generated method stub
        return authorizeMapper.list(obj);
    }

    @Override
    public int doSave(String appCode, List<User> userLists) throws Exception {
        for (User user : userLists) {

            Authorize authorize = new Authorize();
            authorize.setAppCode(appCode);
            authorize.setUser(user.getUserId());
            authorize.setCreateDate(new Date(System.currentTimeMillis()));
            authorizeMapper.save(authorize);
        }
        return 0;
    }

    @Override
    public void deleteAuth(Authorize obj) throws Exception {
        // TODO Auto-generated method stub
        authorizeMapper.deleteAuth(obj);
    }

}
